import Vue from 'vue'

import Vuex from 'vuex'

import VueResource from 'vue-resource'

Vue.use(Vuex)
Vue.use(VueResource)

Vue.config.productionTip = false

// 设置请求的根路径
Vue.http.options.root = './static/data'


// import { Header, Swipe, SwipeItem, Button, Lazyload  } from 'mint-ui'
// import 'mint-ui/lib/style.css'
// Vue.component(Header.name, Header)
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);
import MintUi from 'mint-ui'
Vue.use(MintUi);
import 'mint-ui/lib/style.css'


import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
import './assets/css/app.css'

import VuePreview from 'vue-preview'
Vue.use(VuePreview)

import app from './app.vue'

import router from './router/index'
import filters from './extension/filters'

Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key]);
});

var car = JSON.parse(localStorage.getItem('car')) || [];
var store = new Vuex.Store({
  state: {
    car: car
  },
  mutations: {
    addToCar(state, goods) {
      var flag = false;
      state.car.some(item => {
        if (item.id === goods.id) {
          item.count += parseInt(goods.count);
          flag = true;
          return true;
        }
      });
      if (!flag) state.car.push(goods);

      localStorage.setItem('car', JSON.stringify(state.car));
    },
    updataGoodsInfo(state, goodinfo) {
      console.log(goodinfo);
      state.car.forEach(item => {
        if (item.id === goodinfo.id) {
          item.count = parseInt(goodinfo.count);
          return true;
        }
      });
      localStorage.setItem('car', JSON.stringify(state.car));
    },
    removeFormCar(state, id) {
      state.car.some((item, i) => {
        if (item.id === id) {
          state.car.splice(i, 1);
          return true;
        }
      });
      localStorage.setItem('car', JSON.stringify(state.car));
    },
    updataGoodsSelected(state, goodinfo) {
      console.log(goodinfo);
      state.car.forEach(item => {
        if (item.id === goodinfo.id) {
          item.selected = goodinfo.selected;
          return true;
        }
      });
      localStorage.setItem('car', JSON.stringify(state.car));
    }
  },
  getters: {
    gerAllCount(state) {
      var c = 0;
      state.car.forEach(item => {
        c += parseInt(item.count);
      });
      return c;
    },
    getGoodCount(state) {
      var u = {};
      state.car.forEach(item => {
        u[item.id] = item.count;
      });
      return u;
    },
    getGoodSelect(state) {
      var s = {};
      state.car.forEach(item => {
        s[item.id] = item.selected;
      });
      return s;
    },
    getGoodsCountAndAmount(state) {
      var t = {
        count: 0,
        amount: 0
      };
      state.car.forEach(item => {
        if (item.selected) {
          t.count += parseInt(item.count);
          t.amount += (parseInt(item.price) * parseInt(item.count));
        }
      });
      return t;
    }
  }
});

var vm = new Vue({
  el: '#app',
  render: c => c(app),
  router,
  store
})